#include "lrcpage.h"
#include "ui_lrcpage.h"

#include <QPushButton>
#include <QFile>
#include <QDebug>


LrcPage::LrcPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LrcPage)
{
    ui->setupUi(this);

    //设置窗口无标题栏
    setWindowFlag(Qt::FramelessWindowHint);

    animation = new QPropertyAnimation(this , "geometry" , this);
    animation->setDuration(250);
    animation->setStartValue(QRect(10 , 10 , width() , height()));
    animation->setEndValue(QRect(10 , 10 + height(), width() , height()));

    connect(ui->hideBtn , & QPushButton::clicked , this , [=](){
        animation->start();
    });

    connect(animation , &QPropertyAnimation::finished , this , [=](){
        hide();
    });

    ui->hideBtn->setIcon(QIcon(":/images/xiala.png"));
}

LrcPage::~LrcPage()
{
    delete ui;
}

bool LrcPage::parseLrc(const QString & lrcPath)
{
    QFile lrcFile(lrcPath);

    if(!lrcFile.open(QFile::ReadOnly))
    {
        qDebug()<<"打开⽂件:" << lrcPath;
        return false;
    }

    //清空之前歌词
    lrcLines.clear();

    while(!lrcFile.atEnd())
    {
        QString lrcWord = lrcFile.readLine();

        int left = lrcWord.indexOf('[');
        int right = lrcWord.indexOf(']');

        //解析时间
        int start = 0 , end = 0;
        qint64 lineTime = 0;
        QString time;

        time = lrcWord.mid(left , right - left + 1);

        //解析分钟
        start = 1;
        end = time.indexOf(':');
        lineTime += time.mid(start , end - start).toInt() * 60 * 1000;

        //解析秒
        start = end + 1;
        end = time.indexOf('.' , start);
        lineTime += time.mid(start , end - start).toInt() * 1000;

        //解析毫秒
        start = end + 1;
        end = time.indexOf('.' , start);
        lineTime += time.mid(start , end - start).toInt();

        //解析歌词
        QString word = lrcWord.mid(right + 1).trimmed();

        lrcLines.push_back(LrcLine(lineTime , word.trimmed()));

//        for(auto word : lrcLines)
//        {
//            qDebug()<<word._time<<" "<<word._text;
//        }
    }

    return true;
}

void LrcPage::showLrcWord(qint64 time)
{
    int index = getLineLrcWordIndex(time);

    if(index == -1)
    {
        ui->line1->setText("");
        ui->line2->setText("");
        ui->line3->setText("");
        ui->lineCenter->setText("当前歌曲⽆歌词");
        ui->line4->setText("");
        ui->line5->setText("");
        ui->line6->setText("");
    }
    else
    {
        ui->line1->setText(getLineLrcWord(index-3));
        ui->line2->setText(getLineLrcWord(index-2));
        ui->line3->setText(getLineLrcWord(index-1));
        ui->lineCenter->setText(getLineLrcWord(index));
        ui->line4->setText(getLineLrcWord(index+1));
        ui->line5->setText(getLineLrcWord(index+2));
        ui->line6->setText(getLineLrcWord(index+3));
    }
}

int LrcPage::getLineLrcWordIndex(qint64 time)
{
    if(lrcLines.empty())
    {
        return -1;
    }

    for(int i = 1 ; i < lrcLines.size() ; ++i)
    {
        if(time >= lrcLines[i-1]._time && time < lrcLines[i]._time)
            return i-1;
    }

    return lrcLines.size() - 1;
}

QString LrcPage::getLineLrcWord(int index)
{
    if(index < 0 || index >= lrcLines.size())
        return "";

    return lrcLines[index]._text;
}
